"""empty message

Revision ID: cfcdd57e169a
Revises: 4fb154859865
Create Date: 2019-10-29 22:11:00.257689

"""
from alembic import op
from alembic.operations import Operations
from sqlalchemy.engine import Connection
import sqlalchemy as sa
import ormtypes
from sqlalchemy.dialects import mysql
from models import PermissionGroup, User
# revision identifiers, used by Alembic.
revision = 'cfcdd57e169a'
down_revision = '4fb154859865'
branch_labels = None
depends_on = None


def upgrade():
    conn: Connection = op.get_bind()
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('permission_groups',
                    sa.Column('id', sa.String(length=20), nullable=False),
                    sa.Column('name', sa.String(length=50), nullable=False),
                    sa.Column('permissions', ormtypes.json_pickle.JsonPickle(),
                              nullable=False),
                    sa.PrimaryKeyConstraint('id')
                    )

    op.alter_column('contest', 'judge_result_visible',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=False)
    op.alter_column('contest', 'ranklist_visible',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=False)
    op.alter_column('contest', 'rated',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=False)
    op.alter_column('discussions', 'top',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=False)
    op.alter_column('problems', 'can_see_results',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=False)
    op.alter_column('problems', 'public',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=True)
    op.alter_column('problems', 'using_file_io',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=True)
    op.alter_column('submissions', 'public',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=True)
    op.add_column('users', sa.Column('permission_group',
                                     sa.Text(length=20), nullable=False))
    op.add_column('users', sa.Column(
        'permissions', ormtypes.json_pickle.JsonPickle(), nullable=False))
    op.alter_column('users', 'banned',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=False)
    op.alter_column('users', 'is_admin',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=True)
    op.alter_column('users', 'raw_admin',
                    existing_type=mysql.TINYINT(display_width=1),
                    type_=sa.Boolean(),
                    existing_nullable=False)
    # 默认的权限组
    # conn.execute(
    #     PermissionGroup.__table__.insert(),
    #     [
    #         {
    #             "id": "default", "name": "普通用户", "permissions": ["post.discussion.global", "post.discussion.problem.global", "post.discussion.problem.*"]
    #         },
    #         {
    #             "id": "admin", "name": "管理员", "permissions": ["*"]
    #         },

    #     ]
    # )
    conn.execute("""
        INSERT INTO permission_groups (id,name,permissions) values ("default","普通用户",'["post.discussion.global", "post.discussion.problem.global", "post.discussion.problem.*"]')
    """)
    conn.execute("""
        INSERT INTO permission_groups (id,name,permissions) values ("admin","管理员",'["*"]')
    """)
    conn.execute("""
        update users set permissions="[]" where 1=1
        """)
    conn.execute("""
        update users set permissions='["permission.manage"]' where raw_admin=1
        """)
    conn.execute("""
        update users set permission_group='admin' where raw_admin=1
        """)

    conn.execute("""
        update users set permission_group='default' where raw_admin!=1
        """)

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('users', 'raw_admin',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=False)
    op.alter_column('users', 'is_admin',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=True)
    op.alter_column('users', 'banned',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=False)
    op.drop_column('users', 'permissons')
    op.drop_column('users', 'permission_group')
    op.alter_column('submissions', 'public',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=True)
    op.alter_column('problems', 'using_file_io',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=True)
    op.alter_column('problems', 'public',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=True)
    op.alter_column('problems', 'can_see_results',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=False)
    op.alter_column('discussions', 'top',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=False)
    op.alter_column('contest', 'rated',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=False)
    op.alter_column('contest', 'ranklist_visible',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=False)
    op.alter_column('contest', 'judge_result_visible',
                    existing_type=sa.Boolean(),
                    type_=mysql.TINYINT(display_width=1),
                    existing_nullable=False)
    op.drop_table('permission_groups')
    # ### end Alembic commands ###
